// pages/write-off/choose-traveler.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData(options)
    },
    onShow() {
        this.loadData()
    },
    addTraveler() {
        wx.navigateTo({
            url: '/pages/write-off/traveler-input',
        })
    },
    update(evt) {
        let index = evt.currentTarget.dataset.index
        let current = this.data.travelerList[index]
        let params = {
            object_id: current.object_id,
            consignee:current.real_name,
            id_card: current.card_value,
            tel: current.tel,
        }
        let paramsArr = []
        Object.keys(params).forEach(k => {
            paramsArr.push(k + "=" + params[k])
        })
        wx.navigateTo({
            url: '/pages/write-off/update-traveler' + '?' + paramsArr.join('&'),
        })
    },
    deleteTraveler(evt) {
        let index = evt.currentTarget.dataset.index
        let object_id = this.data.travelerList[index].object_id
        if (!object_id) return
        wx.showModal({
            title: '温馨提示',
            content: '您确定要删除此出行人吗?',
            cancelText: '再想想',
            confirmText: '残忍拒绝',
            confirmColor: '#ff5400',
            success: res => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    app.http.post('hx-goods/delete-member-object', {
                        data: {
                            object_id
                        }
                    }).then(res => {
                        if (res.data.res) {
                            this.data.travelerList.splice(index, 1)
                            this.setData({
                                travelerList: this.data.travelerList
                            })
                            wx.showToast({
                                icon: 'none',
                                title: '删除成功',
                            })
                        }
                    })
                }
            }
        })
    },
    loadData() {
        app.http.get('hx-goods/member-object-list').then(res => {
            this.setData({
                travelerList: res.data.list.map(o => {
                    o.checked = false
                    return o
                })
            })
        })
    },
    /**
     * 多项选择
     */
    checkboxChange: function(e) {
        let travelerList = this.data.travelerList,
            num = this.data.num,
            values = e.detail.value

        if (values.length > num) {
            this.setData({
                travelerList: travelerList.map(o => {
                    if (!o.checked) {
                        o.disable = true
                    }
                    return o
                })
            })
            return
        }
        for (let i = 0; i < travelerList.length; ++i) {
            travelerList[i].checked = travelerList[i].disable = false;
            for (let j = 0; j < values.length; ++j) {

                if (travelerList[i].object_id == values[j]) {
                    travelerList[i].checked = true;
                    break;
                }
            }
        }
        this.setData({
            travelerList,
        });
    },
    submit() {
        if (!app.travelerList) app.travelerList = []
        let arr = []
        arr.push(...this.data.travelerList.filter(o => {
            return o.checked == true
        }))
        if (arr.length == 0) {
            wx.showToast({
                icon: 'none',
                title: '请先选择出行人',
            })
            return
        }
        if (arr.length < this.data.num) {
            let d_value = parseInt(this.data.num) - arr.length
            wx.showToast({
                icon: 'none',
                title: '还差' + d_value + '出行人',
            })
            return
        }
        app.travelerList.push(...arr.map(o => {
            return o.object_id
        }))
        wx.navigateBack({})
    }
})